package cn.samler.project.guestbook.util;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

public class ContextHolderUtil {
    public static HttpServletRequest mockHttpServletRequest = null;

    public static HttpServletRequest getRequest() {
        if (mockHttpServletRequest == null) {
            return ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
        }

        return mockHttpServletRequest;
    }

    public static Object getCurrentUser() {
        return getRequest().getAttribute("current_user");
    }

}
